package indi.kider.server.woniubus.service;

import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.bill.CntReport;
import indi.kider.common.model.woniubus.CntReportVo;
import indi.kider.server.apabus.mapper.RentMapper;
import indi.kider.server.apartment.mapper.CommunityMapper;
import indi.kider.server.bill.mapper.CntReportMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
@SuppressWarnings("ALL")
@Slf4j
public class CntReportServiceImpl implements CntReportService {


    @Autowired
    private CntReportMapper cntReportMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private RentMapper rentMapper;

    /**
     * 抽取公寓每日入住率
     */
    @Transactional(rollbackFor=Exception.class)
    public CommonResponse batchInsertCntReport(){
        try{
            log.info("每日抽取公寓数据开始====start====》");
            Date yestoday = new Date();
//            String format = "yyyy-MM-dd";
//            SimpleDateFormat formatter = new SimpleDateFormat(format);
//            Date yestoday = parserDate(getTimeStr(today,-1,format),format);
            //早上1点运行统计的数据当昨天使用==========================
            CntReport del = new CntReport();
            del.setDay(yestoday);
            cntReportMapper.delete(del);
            List<Community> list = communityMapper.selectAll();
            for(Community community : list){
                CntReport cntReport = new CntReport();
                cntReport.setDay(yestoday);
                cntReport.setCommunityId(community.getId());
                //====================当天入住数量============================
                Rent rentIn = new Rent();
                rentIn.setCommunityId(community.getId());
                rentIn.setRentDate(yestoday);
                int inNum = rentMapper.selectCount(rentIn);
                cntReport.setInNum(inNum);
                //====================当天搬离数量============================
                Rent rentOut = new Rent();
                rentOut.setCommunityId(community.getId());
                rentOut.setLeaveDate(yestoday);
                int outNum = rentMapper.selectCount(rentOut);
                cntReport.setOutNum(outNum);

                cntReportMapper.batchInsertCntReport(cntReport);
            }
        }catch (Exception e){
            log.info("每日抽取公寓数据出现异常====error====》"+e.getMessage());
        }finally {
            log.info("每日抽取公寓数据结束====end====》");
        }
        return CommonResponse.buildUpdateSuccessResponse("每日抽取公寓数据完成!",null);
    }

    @Override
    public CommonResponse selcetDataByDay(CntReportVo cntReportVo) {
        return CommonResponse.buildDefaultSuccessResponse("查询成功",cntReportMapper.selcetDataByDay(cntReportVo));
    }

    @Override
    public CommonResponse selcetDataByMonth(CntReportVo cntReportVo) {
        return CommonResponse.buildDefaultSuccessResponse("查询成功",cntReportMapper.selcetDataByMonth(cntReportVo));
    }

    @Override
    public CommonResponse selcetDataByMonthGroupByYear(CntReportVo cntReportVo) {
        return CommonResponse.buildDefaultSuccessResponse("查询成功",cntReportMapper.selcetDataByMonthGroupByYear(cntReportVo));
    }






    /**
     * 方法说明：获取给定时间的前后日期
     * @param date
     * @param offset 偏移量
     * @param format
     * @return
     */
    public static String getTimeStr(Date date,int offset,String format){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, offset);
        return formatDate(cal.getTime(),format);
    }
    public static Date parserDate(String dateStr, String format) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(dateStr);
    }
    public static String formatDate(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }
}
